package cn.school.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @author oldliu
 * @since 1.0
 */
public class HelloServlet extends HttpServlet {
    public HelloServlet(){
        System.out.println("HelloServlet对象创建了");
    }
    String clsName;
    @Override
    public void init() throws ServletException {
        System.out.println("HelloServlet 初始化了");
        clsName=super.getInitParameter("clsName");

    }

    @Override
    public void destroy() {
        System.out.println("HelloServlet 被销毁了");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("当前的请求方式："+req.getMethod());
        super.service(req,resp);//!!!!!!!!!!!!!!!!!!!!!
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //等同于jsp的内置对象out
        resp.setContentType("text/html;charset=UTF-8");
        //相当于jsp中的 application
        ServletContext servletContext = req.getServletContext();
        servletContext.setAttribute("tongzhi","后天才是植树节");
        PrintWriter out = resp.getWriter();
        String brower=req.getHeader("user-agent");
//        if(brower.contains("Gecko")){
//            out.print("你这个浏览器已经不支持了，走吧");
//            return;
//        }

        out.write("<html>");
        out.write("<head>");
        out.write("<script>");
        //out.write("alert('大扎好，我司渣渣灰');");
        out.write("</script>");
        out.write("</head>");

        out.write("<body>");
        out.write("通知："+servletContext.getAttribute("tongzhi"));
        out.write("<h1>");
        out.write(clsName+"的谢旭东是个好同志！这是一个get请求");
        out.write("</h1>");
        out.print("<h1>"+servletContext.getAttribute("tongzhi")+"</h1>");
        Enumeration<String> e = req.getHeaderNames();
        while (e.hasMoreElements()){
            String key=e.nextElement();
            String val=req.getHeader(key);
            out.print(key+"="+val+"<br>");
        }

        out.write("</body>");
        out.write("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //等同于jsp的内置对象out
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.write("<html>");
        out.write("<head>");
        out.write("<script>");
        //out.write("alert('大扎好，我司渣渣灰');");
        out.write("</script>");
        out.write("</head>");
        out.write("<body>");
        out.write("<h1>");
        out.write(clsName+"的孙楠楠是个好同志！这是一个post请求");
        out.write("</h1>");

        out.write("</body>");
        out.write("</html>");
    }
}
